Lab 8: The Twelve Days of Christmas

Sing the Full Song

Author

Your name

library(tidyverse)
xmas <- read.csv("https://raw.githubusercontent.com/zoerehnberg/STAT331-S23/main/practice_activities/xmas.csv")

Download starter .qmd file

You’ve already created two helper functions, pluralize_gift() and make_phrase(), which you used to create a column of song phrases in the practice activity.

Today, you will write a function that sings the appropriate lines of the song for a given day. We will then use this function to iterate through the 12 days of Christmas to sing the full song!

1 Helper Functions

Note

You will need to copy over your functions from the practice activity into your new lab Quarto document.

# Copy over functions from your practice activity!

Use your PA functions to create a Full.Phrase column in the xmas2 dataset.

xmas2 <- xmas |>
  mutate(
    Full.Phrase = pmap_chr(.l = list(num       = Day,
                                     item      = Gift.Item, 
                                     verb      = Verb, 
                                     adjective = Adjective, 
                                     location  = Location), 
                           .f = make_phrase))
Error in `mutate()`:
ℹ In argument: `Full.Phrase = pmap_chr(...)`.
Caused by error:
! object 'make_phrase' not found

2 Iteration

Write a function called sing_day() that takes as input:

  • A dataset (input as a dataframe).
  • A number indicating which day to sing about (input as an integer).
  • The name of a column in the dataset that contains the phrases for each day (input as a tidy name).

For example,

sing_day(xmas2, 2, Full.Phrase)

should return

On the second day of Christmas, my true love sent to me:
two turtle doves and
a partridge in a pear tree.
Tip

Hint 1: The {phrase_col} part, which I provided for you, lets you use column names as arguments to a function. Don’t delete that line of code!

Hint 2: The ordinal() function from the english package could be helpful when converting the line input (e.g., 1) to a word (e.g., “first”).

# Produces the string for one day of the song.
# Arguments:  dataset -- a dataframe containing information about gifts.
#             num -- the number of the day you want to sing about.
#             phrase_col -- the variable name for the column in dataset with the song phrases.
# Returns:    a string with the line of the song containing ALL gifts for the given day.

sing_day <- function(dataset, num, phrase_col){
  
  # Step 1: Setup the intro line
  # Hint: You need to convert a number (e.g., 1) to a word (e.g., first)
  num_word <- ____
  
  intro <- glue::glue("On the {num_word} day of Christmas, my true love sent to me:")
  
  # Step 2: Sing the gift phrases
  # Hint: What order are they gifts sung in each day?

    phrases <- dataset |>
      pull({{phrase_col}})

  ????
    
  ## put it together
    
  ????

}
Error in parse(text = input): <text>:11:16: unexpected input
10:   # Hint: You need to convert a number (e.g., 1) to a word (e.g., first)
11:   num_word <- __
                   ^
A Small Test
xmas2 |>
  sing_day(num = 2, phrase_col = Full.Phrase)
Error in sing_day(xmas2, num = 2, phrase_col = Full.Phrase): could not find function "sing_day"

3 Sing the Song

Run the following code to test out your functions! The output should be the lyrics for the entire The Twelve Days of Christmas song.

map_chr(1:12, ~ sing_day(dataset = xmas2, 
                         num = .x, 
                         phrase_col = Full.Phrase)) |>
  str_c(collapse = "\n") |>
  cat()
Error in `map_chr()`:
ℹ In index: 1.
Caused by error in `sing_day()`:
! could not find function "sing_day"
Warning

You will get automatic deductions for:

  • Functions that do not work the way they are intended.
  • Hard coding values in your in functions.
  • Not using the map() code supplied for you to iterate the function you just wrote.
  • Not singing the full song. Remember, each day, you get the gift for that day and all the prior days.

4 Style the Song Output

Let’s output the song in a visually appealing way!

Note

Make the following changes to your sing_day() function above. Do not copy the code to a new code chunk for the style section.

a. Remove any additional whitespace.

  • There should be only one whitespace character between words.
  • There should be no whitespace at the beginning or end of each phrase.

b. Print each phase on its own line.

  • For example, your function should output the following for sing_day(xmas, num = 2, phrase_col = Full.Phrase):
On the second day of Christmas, my true love sent to me: 
two turtle doves and 
a partridge in a pear tree

c. Add blank lines between the different lines of the song.

  • For example, your output should look like the following:
On the first day of Christmas, my true love sent to me:
a partridge in a pear tree

On the second day of Christmas, my true love sent to me: 
two turtle doves and 
a partridge in a pear tree

d. Ensure the lines of your song are grammatically correct.

  1. Use of Commas – each line should end in a comma except for the last line.
  2. Use of And – there should be an “and” included either at the end of the second to last line or at the beginning of the final line.
  3. Use of Period – there should be a period at the end of the final line.
  • For example, your function should output the following for sing_day(xmas, num = 3, phrase_col = Full.Phrase):
On the third day of Christmas, my true love sent to me: 
three french hens, 
two turtle doves and 
a partridge in a pear tree.
Note

While I am a fan of the Oxford comma, you do not have to use an Oxford comma for the second to last phrase of your song.